package com.kxf.bsdiff;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;

/**
 * @ProjectName: AndroidTestDemo
 * @Package: com.kxf.bsdiff
 * @ClassName: Utils
 * @Description: java类作用描述
 * @Author: kuangxuefeng
 * @qq: 1024883177
 * @CreateDate: 2021/3/11 15:34
 */
public class Utils {
    public static String getBasePath() {
        return Environment.getExternalStorageDirectory().getPath() + File.separator + "adiff" + File.separator;
    }

    public static void printFile(String fPath) {
        File file = new File(fPath);
        if (file.exists()) {
            try (FileInputStream fileInputStream = new FileInputStream(file)) {
                byte[] bs = new byte[1024];
                int len = -1;
                StringBuilder sb = new StringBuilder();
                while ((len = fileInputStream.read(bs)) > 0) {
                    sb.append(new String(bs, 0, len));
                }
                System.out.println("=======================" + fPath);
                System.out.println(sb.toString());
                System.out.println("=======================" + fPath);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static void copyAssetsToFile(Context context, String from, String to) {
        File fileTo = new File(to);
        if (!fileTo.getParentFile().exists()) {
            fileTo.getParentFile().mkdirs();
        }
        try (InputStream InputStream = context.getAssets().open(from);
             FileOutputStream fileOutputStream = new FileOutputStream(to)) {
            byte[] bs = new byte[1024];
            int len = -1;
            while ((len = InputStream.read(bs)) > 0) {
                fileOutputStream.write(bs, 0, len);
            }
            System.out.println("copyAssetsToFile finish  " + to);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getFileMD5(String path) {
        BigInteger bi = new BigInteger("-1");
        File f = new File(path);
        try(FileInputStream fis = new FileInputStream(f)) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[8192];
            int len = 0;
            while ((len = fis.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            byte[] b = md.digest();
            bi = new BigInteger(1, b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bi.toString(16);
    }

    /**
     * 通过包名获取apk信息，packageInfo.applicationInfo.sourceDir获取apk的备份路径
     * @param context
     * @param packageName
     * @return
     */
    public static PackageInfo getApkSourceDir(Context context, String packageName){
        PackageInfo dir = null;
        PackageManager packageManager = null;
        packageManager = context.getPackageManager();
        List<PackageInfo> mAllPackages = packageManager.getInstalledPackages(0);

        for(int i = 0; i < mAllPackages.size(); i ++) {
            PackageInfo packageInfo = mAllPackages.get(i);
            if (TextUtils.equals(packageInfo.packageName, packageName)){
                dir = packageInfo;
                break;
            }
//            Log.i("package path", packageInfo.applicationInfo.sourceDir);
//            Log.i("apk name", packageInfo.applicationInfo.loadLabel(packageManager).toString());
        }
        return dir;
    }
}